package com.hzlj.position.config.config;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import com.hzlj.common.message.common.util.AudioConvertRestUtil;
import com.hzlj.common.message.common.util.MessageRestUtil;
import com.hzlj.common.message.common.util.PhoneStatusRestUtil;
import com.hzlj.common.message.common.util.VoiceprintRestUtil;
import com.hzlj.position.config.common.dto.common.PositionHandleExtDTO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author lifh
 * @date 2022/2/27 16:47
 **/
@Data
@Configuration
@ConfigurationProperties("position")
public class PositionConfig {
    /**
     * 是否启用运营商定位任务
     */
    private boolean operatorTask = true;
    /**
     * 是否启用腕带定位任务
     */
    private boolean wdTask = true;
    /**
     * 定位新增数据的批量size
     */
    private Integer addBatchSize = 1;
    /**
     * 定位新增数据的批量秒
     */
    private Integer addBatchSeconds = 10;
    /**
     * 定位新增数据的批量秒
     */
    private Integer positionNewestCacheSeconds = 60;

    /**
     * common服务配置
     */
    private CommonService commonService;

    /**
     * stat服务配置
     */
    private StatService statService;
    /**
     * 使用机构的边界
     */
    private boolean useJgBj = true;
    /**
     * 在请假+外出中是否需要忽略越界的jgCodes
     */
    private String[] ignoreCrossInLeaveJgCodes;
    /**
     * 哪些定位需要显示已读未读情况
     * 默认 越界/拆卸
     */
    private String readWarningTypes = "2,4";
    /**
     * 同步地址
     */
    private String dbSyncUrl = "http://localhost:10112/position";
    /**
     * 可以越界距离
     */
    private BigDecimal crossDistance = BigDecimal.ZERO;
    /**
     * 可以越界距离(外出中)
     */
    private BigDecimal leavingCrossDistance = BigDecimal.ZERO;
    /**
     * 低电报警
     */
    private Integer lowBat = 20;
    /**
     * 外出时，是否需要同步位置
     */
    private Boolean syncPositionIfLeaving = false;
    /**
     * 重新定位配置
     */
    private ReLocate reLocate = new ReLocate();
    /**
     * 纠偏配置
     */
    private Correct correct = new Correct();
    /**
     * 人脸签到配置
     */
    private Initiative initiative = new Initiative();
    /**
     * 点验签到配置
     */
    private Inspection inspection = new Inspection();
    /**
     * 矫正业务配置
     */
    private Jz jz = new Jz();
    /**
     * 语音上报
     */
    private Audio audio = new Audio();

    /**
     * 系统处理为正常
     */
    private PositionNormalHandle positionNormalHandle = new PositionNormalHandle();

    /**
     * 异常配置
     */
    private Abn abn = new Abn();

    /**
     * 经纬度相关
     */
    private LonLat lonLat = new LonLat();

    /**
     * 通知作业
     */
    private NoticeJobQueue noticeJobQueue = new NoticeJobQueue();

    @Data
    public static class NoticeJobQueue {
        private Integer size = 1000;
        private Integer thread = 1;
        private Integer fixedDelaySecond = 2;
    }


    @PostConstruct
    private void init() {
        MessageRestUtil.init(commonService.url);
        AudioConvertRestUtil.init(commonService.url);
        PhoneStatusRestUtil.init(commonService.url);
        VoiceprintRestUtil.init(commonService.url);
    }

    @Data
    public static class Abn {
        /**
         * 是否排除免考核的人
         */
        private Boolean excludeMkh = true;
        /**
         * 是否排除强制措施
         */
        private Boolean excludeQzcs = true;
        /**
         * 是否开启聚集
         */
        private Boolean gathering = true;
        /**
         * 聚集多少人开始算
         */
        private Integer minGatheringPersonNum = 10;
        /**
         * 开始时间
         */
        private String nightActivityStartTime = "00:00:00";
        /**
         * 结束时间
         */
        private String nightActivityEndTime = "06:00:00";
        /**
         * 是否开启失联
         */
        private Boolean missing = false;
        private Integer missingNoticeThread = 3;
        /**
         * 开始时间
         */
        private String missingStartTime = "08:00:00";
        /**
         * 结束时间
         */
        private String missingEndTime = "08:00:00";
        /**
         * 需要使用的jgCodes
         */
        private String[] missingJgCodes;


        public boolean nightActivityYesterday() {
            return nightActivityStartTime.compareTo(nightActivityEndTime) >= 0;
        }

        public boolean missingYesterday() {
            return missingStartTime.compareTo(missingEndTime) >= 0;
        }

    }


    /**
     * 机构隶属层级-越界类型
     */
    private Map<String, String> areaOutTypes = MapUtil.of(
            new Pair<>("02", "1"),//省
            new Pair<>("03", "2"),//市
            new Pair<>("04", "3")//区县
    );


    public Map<String, String> getAreaOutTypes() {
        return areaOutTypes;
    }

    @Data
    public static class CommonService {
        private String url;
        private Integer thread = 3;
    }

    @Data
    public static class Notice {
        private String url;
    }

    @Data
    public static class StatService {
        private String url;
    }

    @Data
    public static class ReLocate {
        /**
         * 重新定位的间隔秒数
         */
        private Integer seconds = 60 * 10;
        ;
        /**
         * 重新定位的次数=0代表不重试
         */
        private Integer count = 1;
        /**
         * 重新定位之后，是否发起核查
         */
        private Boolean inspection = true;
    }

    @Data
    public static class Correct {
        /**
         * 纠偏的距离
         */
        private BigDecimal matchDistance = BigDecimal.valueOf(500);
    }

    @Data
    public static class Initiative {
        /**
         * 是否排除免考核的人
         */
        private Boolean excludeMkh = true;
        /**
         * 是否排除强制措施
         */
        private Boolean excludeQzcs = true;
        /**
         * 在每个签到时间段里面是否需要显示越界
         */
        private Boolean displayCrossInReportTimes = true;
    }

    @Data
    public static class Inspection {
        /**
         * 是否排除免考核的人
         */
        private Boolean excludeMkh = true;
        /**
         * 是否排除强制措施
         */
        private Boolean excludeQzcs = true;
    }

    @Data
    public static class Audio {
        /**
         * 转文本
         */
        private Boolean audioText = true;
        private Boolean useAudioNetUrl = true;
    }


    @Data
    public static class Jz {
        /**
         * 是否将定位号码同步到矫正系统中
         */
        private Boolean sjhSync = false;
    }


    @Data
    public static class PositionNormalHandle extends PositionHandleExtDTO {
        private String handleReason = "经核查，矫正对象状态正常，网络问题导致";
        private String handleRemark = "矫正对象位置确认正常，系统自动处理为核实正常";
        private String handlerUid = "system";
        private String handlerName = "系统";
    }

    @Data
    public static class LonLat {
        /**
         * 类型
         * gd:高德
         * tianditu：天地图
         */
        private String types = "tianditu,gd";
        /**
         * 高德
         */
        private String gdUrl = "http://139.196.126.188:19999/LocateCore/locate/getLocate";
        /**
         * 天地图
         */
        private String tiandituUrl = "https://api.tianditu.gov.cn/geocoder";
        /**
         * 天地图的key
         */
        private String tiandituKey;
        /**
         * 重试次数
         */
        private int retryCount = 2;
        private String[] typesArray = types.split(",");

        public void setTypes(String types) {
            this.types = types;
            typesArray = types.split(",");
        }

        public String type(int _retryCount) {
            int count = Math.max(_retryCount - 1, 0) % typesArray.length;
            return typesArray[count];
        }
    }
}
